package com.fangyi.yunmusic.service.es;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.pojo.Artist;
import com.fangyi.yunmusic.pojo.es.AlbumDocument;
import com.fangyi.yunmusic.pojo.es.ArtistDocument;
import com.fangyi.yunmusic.pojo.vo.ArtistVO;
import com.fangyi.yunmusic.repository.es.AlbumRepository;
import com.fangyi.yunmusic.repository.es.ArtistRepository;
import com.fangyi.yunmusic.utils.PagedResult;
import com.fangyi.yunmusic.utils.PojoUtils;
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.WeightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;

@Service
public class ArtistSearchService {

    @Autowired
    private ArtistRepository artistRepository;

    @Autowired
    private ResourceConfig resourceConfig;

    public Page<ArtistDocument> queryArtist(Integer pageNum, Integer pageSize, String query) {

        // 设置查询条件
        FunctionScoreQueryBuilder.FilterFunctionBuilder[] functions = {
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("description", query), new WeightBuilder().setWeight(10)),
                new FunctionScoreQueryBuilder.FilterFunctionBuilder(matchPhraseQuery("name", query), new WeightBuilder().setWeight(90)), // 设置权重
        };
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(functions)
                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)     // 设置权重分求和模式
                .setMinScore(10);   // 设置权重分最低分


        Pageable pageable = PageRequest.of(pageNum, pageSize);// 设置分页

        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(functionScoreQueryBuilder).build();

        return artistRepository.search(nativeSearchQuery);


    }

    public PagedResult queryArtistVO(Integer pageNum, Integer pageSize, String query) {
        Page<ArtistDocument> artistDocumentPage = queryArtist(pageNum-1, pageSize, query);
        List<ArtistDocument> artistDocuments = artistDocumentPage.getContent();
        List<ArtistVO> artistVOs = artistDocuments.stream().map(artistDocument -> {
            ArtistVO artistVO = new ArtistVO();
            PojoUtils.copyProperties(artistDocument, artistVO);
            artistVO.setPicUrl(resourceConfig.getArtistPicUrl(artistDocument.getPicUrlId()));
            return artistVO;
        }).collect(Collectors.toList());
        return new PagedResult(artistVOs, pageNum, artistDocumentPage.getTotalPages(), artistDocumentPage.getTotalElements());
    }


}
